#-*-coding:utf-8-*-

'''
# ----------------------------------------------------------------------------
#  Purpose:     基于 Python 实现的奇技淫巧

#  Author:      青衫少年春归也

#  Created:     2020.01.11

# ----------------------------------------------------------------------------
'''

import time
import turtle
import pygame

class HeartShape():
    def draw_heart(self):
        """
        手动画一个心型图案
        :return:
        """
        self.turtle = turtle.Turtle()
        self.turtle.color("red", "pink")
        self.turtle.begin_fill()
        self.turtle.left(140)
        self.turtle.forward(111.65)
        self._curve_move()
        self.turtle.left(120)
        self._curve_move()
        self.turtle.forward(111.65)
        self.turtle.end_fill()

    def _curve_move(self):
        for i in range(200):
            self.turtle.right(1)
            self.turtle.forward(1)

    def draw_heart_with_music(self, music_path:str):
        """
        画心的同时播放音乐
        参数信息：
            :param music_path： 音乐文件路径
        :return:
        """
        # 初始化音频
        pygame.mixer.init()
        # 导入音频文件
        self.track = pygame.mixer.music.load(music_path)
        # 播放音乐
        pygame.mixer.music.play()

        self._prepare_draw()
        self._start_draw()

    def prepare_draw(self):
        """
        准备画线的初始化工作
        :return:
        """
        self.turtle = turtle.Turtle()
        # 隐藏箭头
        self.turtle.hideturtle()
        # 设置背景色
        self.turtle.getscreen().bgcolor("#f0f0f0")
        # 设置画线颜色
        self.turtle.color("#c1e6e6", "red")
        # 画笔大小
        self.turtle.pensize(2)
        # 图形绘制速度
        self.turtle.speed(8)
        # 移动，不划线
        self.turtle.up()
        # 移动到指定位置
        # self.turtle.goto(0, -150)

    def start_draw(self):
        """
        开始画线
        :return:
        """
        self.turtle.down()
        self.turtle.begin_fill()
        # self.turtle.goto(0, -150)
        # self.turtle.goto(-175.12, -8.59)
        self.turtle.left(140)
        self.turtle.forward(111.65)
        pos = []
        for i in range(200):
            self.turtle.right(1)
            self.turtle.forward(1)
            pos.append((-self.turtle.pos()[0], self.turtle.pos()[1]))
        for item in pos[::-1]:
            self.turtle.goto(item)
        # self.turtle.goto(175.12, -8.59)
        # self.turtle.goto(0, -150)
        # self.turtle.left(140)
        # self.turtle.forward(111.65)
        self.turtle.end_fill()

    def end_draw(self):
        """
        暂停音乐的播放，并停止画线
        :return:
        """
        pygame.mixer.music.fadeout(100)
        # self.turtle.done()

    def add_title(self):
        """
        添加标题等其他信息
        :return:
        """
        self.turtle.color("blue")
        self.turtle.up()
        self.turtle.goto(0, 200)
        self.turtle.write("love you ~",
                          font=(u"方正舒体", 36, "normal"),
                          align="center")
        self.turtle.goto(200, -250)
        self.turtle.write("by 青衫少年春归也 ~", font=(u"方正舒体", 10, "bold"))


if __name__ == '__main__':
    t = HeartShape()
    t.prepare_draw()
    t.start_draw()
    t.add_title()
    time.sleep(45)